package cn.bytets.rpc.common.utils;

import java.lang.reflect.Modifier;

public class ClassGeneratorUtils {


    public static void getModifiers(int modifiers,StringBuffer buffer){
        if(Modifier.isPublic(modifiers)){
            buffer.append(" public");
        } else if(Modifier.isPrivate(modifiers)){
            buffer.append(" private");
        } else if(Modifier.isProtected(modifiers)){
            buffer.append(" protected");
        } else {
            buffer.append(" public");
        }
        if (Modifier.isStatic(modifiers)) {
            buffer.append(" static");
        }
        if (Modifier.isVolatile(modifiers)) {
            buffer.append(" volatile");
        }

    }
    public static String asArgument(Class<?> cl, String name) {
        if (cl.isPrimitive()) {
            if (Boolean.TYPE == cl) {
                return name + "==null?false:((Boolean)" + name + ").booleanValue()";
            }
            if (Byte.TYPE == cl) {
                return name + "==null?(byte)0:((Byte)" + name + ").byteValue()";
            }
            if (Character.TYPE == cl) {
                return name + "==null?(char)0:((Character)" + name + ").charValue()";
            }
            if (Double.TYPE == cl) {
                return name + "==null?(double)0:((Double)" + name + ").doubleValue()";
            }
            if (Float.TYPE == cl) {
                return name + "==null?(float)0:((Float)" + name + ").floatValue()";
            }
            if (Integer.TYPE == cl) {
                return name + "==null?(int)0:((Integer)" + name + ").intValue()";
            }
            if (Long.TYPE == cl) {
                return name + "==null?(long)0:((Long)" + name + ").longValue()";
            }
            if (Short.TYPE == cl) {
                return name + "==null?(short)0:((Short)" + name + ").shortValue()";
            }
            throw new RuntimeException(name + " is unknown primitive type.");
        }
        return "(" + ReflectUtils.getName(cl) + ")" + name;
    }
}
